package com.jsnkj.basic.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.RewardTaskAdapter
import com.jsnkj.basic.advertisement.AdvertisementManager
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivityInviteFriendsBinding
import com.jsnkj.basic.mvvm.BaseKtActivity
import com.jsnkj.basic.util.ErrorResultUtil


/**
 * 邀请好友
 */
class InviteFriendsActivity :
    BaseKtActivity<ActivityInviteFriendsBinding, InviteFriendsViewModel>(), View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_invite_friends


    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this


        mVM.errorResult.observe(this) {
            dismissDialog()

            ErrorResultUtil.errorResultToast(this@InviteFriendsActivity, it)

        }

        mVM.postHttpResult.observe(this) {
            dismissDialog()
            if (it.share_link.isNullOrEmpty()) {
                com.jsnkj.basic.util.ToastUtils.show("邀请失败")
                return@observe
            }
            GoInviteActivity.goToInvite(this@InviteFriendsActivity, it.share_link + "&cid=shagnwan")
        }


        mVM.postUserEarningsResult.observe(this) {
            mVDB.txtNumber.text = "${it.invites}人"
            mVDB.txtMakeMoney.text = "${it.earnings}元"
            mVDB.txtWithdrawalMoney.text = "${it.withdrawable}元"
        }

        initRecyclerViewData()
        mVDB.tv2.requestFocus()

        AdvertisementManager.showLoadInteraction(this, SpConfig.AD_TIME)
    }

    private fun initRecyclerViewData() {
        mVM.initRewardTaskList()

        mVDB.rvRewardTask.apply {
            layoutManager =
                LinearLayoutManager(this@InviteFriendsActivity, LinearLayoutManager.VERTICAL, false)

            com.jsnkj.basic.util.LogUtils.e("邀请数据L：${mVM.rewardTaskList.size}")
            adapter = RewardTaskAdapter(this@InviteFriendsActivity, mVM.rewardTaskList)
        }

        if (mVM.rewardTaskList.isNullOrEmpty()) {
            mVDB.cardViewRewardTask.visibility = View.GONE
        } else {
            mVDB.cardViewRewardTask.visibility = View.VISIBLE
        }

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.image_return -> {
                onBackPressed()
            }

            R.id.tv_invite -> {
                showDialog()
                mVM.postUserInviteCode()
            }

            R.id.txt_invite_detail -> {
                com.jsnkj.basic.util.ToastUtils.show("邀请详情")
            }

            R.id.txt_withdrawal -> {
                startActivity(WithdrawActivity::class.java)
            }
        }

    }


    override fun onResume() {
        super.onResume()

        mVM.postUserEarnings()


    }

}